#include "SWM341.h"
#include "user_app.h"
#include "dev_uart.h"
static uint8_t Get_match_index(uint16_t Command);
static void Get_bms_info(uint16_t cmd);
static void Get_send_order_data(uint8_t page_index, uint8_t index);
static void BLE_CMD_Process(uint8_t *p_data, uint8_t get_lenth);
static void DMA_UART_BLUE_TX_Config(uint8_t *send_buffer, uint16_t send_len);
static void DMA_UART_TX_Config(uint8_t *send_buffer, uint16_t send_len);
static void send_BLE_GET_NAME_order(void);
static void send_BLE_GET_MAC_order(void);
void send_BLE_Stop_Scan_order(void);
void send_BLE_DISCON_order(void);
void eeprom_write(uint32_t addr, uint8_t data);
void config_read(void);
void send_BLE_Connect_order(def_ble_device_type ble_device);
void send_BLE_Scan_order(void);
void send_BLE_NAME_order( char *NAME_DATA);
unsigned int Getlen(const char *p1);
int Instr(uint8_t *p1, unsigned int  len,  char *p2);
char str_temp[100];
int str3;
uint8_t auto_connect_time;
/*
*********************************************************************************************************
    函数原型：unsigned int calccrc(unsigned char crcbuf,unsigned int crc)
    描    述：计算CRC的校验
    输入参数：unsigned char crcbuf,unsigned int crc
    输出参数：-
*********************************************************************************************************
*/
uint16_t calccrc(uint8_t crcbuf, uint16_t crc)
{
    uint8_t i;
    uint8_t tmp;
    crc = crc ^ crcbuf;
    for (i = 0; i < 8; i++)
    {
        tmp = crc & 1;
        crc = crc >> 1;
        if (tmp == 1)
            crc = crc ^ 0xa001;
    }
    return crc;
}

/*
*********************************************************************************************************
    函数原型：unsigned int CrcData(unsigned char* commdata,unsigned int len)
    描    述：CRC校验
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
uint16_t CrcData(uint8_t *commdata, uint16_t len)
{
    uint16_t  i;
    uint16_t  comcrc;
    comcrc = 0xffff;
    for (i = 0; i < len; i++)
    {
        comcrc = calccrc(commdata[i], comcrc);
    }
    return comcrc;
}

/*
*********************************************************************************************************
    函数原型：void UART_Receive(void)
    描    述：UART协议解析
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void Receive_process(UART_TYPE  data_p)
{
    uint16_t order, len, data_start, crc_cmd, get_crc ;
    uint8_t data_temp[60], i;
    uint32_t temp_h, temp_l;
    UART_TYPE uart_temp;
    char str[20];
    uart_temp = data_p;
    #if(BLUE_ENABLE)
    {
        if (s.ble_d)
        {
            s.ble_d = 0;
            // printf("get_data_blue len = %d\r\n", uart_temp.Lenth);
            //            for (int i = 0; i < uart_temp.Lenth; i++)
            //            {
            //                printf("%02X", uart_temp.Data[i]);
            //            }
            //DMA_UART_TX_Config(uart_temp.Data, uart_temp.Lenth);
            BLE_CMD_Process(uart_temp.Data, uart_temp.Lenth);
        }
    }
    #endif
    if (uart_temp.Lenth < 30)
    {
			
			  if ((uart_temp.Data[0] == 0x11)
                    && (uart_temp.Data[1] == 0x22)
                    && (uart_temp.Data[2] == 0x33)
                    && (uart_temp.Data[3] == 0x44)
                    && (uart_temp.Data[4] == 0x55)
                    && (uart_temp.Data[5] == 0x66)
                    && (uart_temp.Data[6] == 0x77)
                    && (uart_temp.Data[7] == 0x88)
               )
            {
							eeprom_write(31, 0xaa);
							NVIC_SystemReset();
						}
						
			 else
			 {
        crc_cmd = CrcData(uart_temp.Data, uart_temp.Lenth - 2);
        get_crc = (uart_temp.Data[uart_temp.Lenth - 1] << 8) + uart_temp.Data[uart_temp.Lenth - 2];
         if (crc_cmd == get_crc)
        {
            if ((uart_temp.Data[0] == HEAD_ID) && (uart_temp.Data[1] == 0x04))
            {
                off_line_time = 0;
                s.off_line = 0;
                //*********************************************
                len = uart_temp.Data[2];
                order = uart_temp.Data[2];
                order = (order << 8);
                order = order + uart_temp.Data[3];
                len = uart_temp.Data[4];
                data_start = 5;
                //**********************************************************
                switch (order)
                {
                case MAIN_ORDER_A:
                {
                    if (len > 1)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 0];
                        data_temp[1] = uart_temp.Data[data_start + 1];
                        bms_main_data.VOLT = data_temp[0] * 256 + data_temp[1] ;
                    }
                    if (len > 3)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 2];
                        data_temp[1] = uart_temp.Data[data_start + 3];
                        bms_main_data.Curr = data_temp[0] * 256 + data_temp[1] ;
                    }
                    if (len > 7)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 6];
                        data_temp[1] = uart_temp.Data[data_start + 7];
                        bms_main_data.SOC = (data_temp[0] * 256 + data_temp[1]) / 10 ;
                    }
                    if (len > 13)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 12];
                        data_temp[1] = uart_temp.Data[data_start + 13];
                        bms_main_data.MaxVolt = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    break;
                }
                case MAIN_ORDER_B:
                {
                    if (len > 1)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 0];
                        data_temp[1] = uart_temp.Data[data_start + 1];
                        bms_main_data.MinVolt = data_temp[0] * 256 + data_temp[1] ;
                    }
                    if (len > 3)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 2];
                        data_temp[1] = uart_temp.Data[data_start + 3];
                        //预留
                    }
                    if (len > 7)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 6];
                        data_temp[1] = uart_temp.Data[data_start + 7];
                        bms_main_data.MaxTemp = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    if (len > 13)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 12];
                        data_temp[1] = uart_temp.Data[data_start + 13];
                        bms_main_data.MinTemp = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    break;
                }
                case GET_SOFT_VER:
                {
                    if (len > 4)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 0];
                        data_temp[1] = uart_temp.Data[data_start + 1];
                        // data_temp[2] = uart_temp.Data[data_start + 2];
                        //data_temp[3] = uart_temp.Data[data_start + 3];
                        temp_h = data_temp[0] * 100;
                        temp_h = temp_h + (data_temp[1] >> 4) * 10 + ((data_temp[1] & 0X0F) % 10);
                        sprintf(str, "%d", temp_h);
                        strcpy(bms_version_data.bms_sw, str);
                    }
                    for (i = 4; (i < 12); i++)
                    {
                        bms_version_data.bms_hw[i - 4] = uart_temp.Data[data_start + i];
                    }
                    for (i = 12; i < len; i++)
                    {
                        bms_version_data.bms_code[i - 12] = uart_temp.Data[data_start + i];
                    }
                    break;
                }
                case GET_ERROR:
                {
                    data_temp[0] = 0;
                    data_temp[1] = 0;
                    if (len > 1)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 1];
                    }
                    if (len > 3)
                    {
                        data_temp[1] = uart_temp.Data[data_start + 3];
                    }
                    bms_main_data.FltCode1 = data_temp[0]  ;
                    bms_main_data.FltCode2 = data_temp[1];
                    break;
                }
                case GET_DATE:
                {
                    uint16_t year, month, date;
                    if (len > 1)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 0];
                        data_temp[1] = uart_temp.Data[data_start + 1];
                        year = data_temp[0] * 256 + data_temp[1] ;
                    }
                    if (len > 3)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 2];
                        data_temp[1] = uart_temp.Data[data_start + 3];
                        month = data_temp[0] * 256 + data_temp[1] ;
                    }
                    if (len > 5)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 4];
                        data_temp[1] = uart_temp.Data[data_start + 5];
                        date = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    sprintf(str, "%d-%d-%d", year, month, date);
                    strcpy(bms_version_data.bms_date, str);
                    break;
                }
                case GET_INFO_A:
                {
                    if (len > 1)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 0];
                        data_temp[1] = uart_temp.Data[data_start + 1];
                        bms_info_data.Uniformity = data_temp[0] * 256 + data_temp[1] ;
                    }
                    if (len > 3)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 2];
                        data_temp[1] = uart_temp.Data[data_start + 3];
                        bms_info_data.SOH = data_temp[0] * 256 + data_temp[1] ;
                    }
                    if (len > 5)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 4];
                        data_temp[1] = uart_temp.Data[data_start + 5];
                        bms_info_data.ODO = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    if (len > 7)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 6];
                        data_temp[1] = uart_temp.Data[data_start + 7];
                        bms_info_data.ODO_Consumption = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    if (len > 9)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 8];
                        data_temp[1] = uart_temp.Data[data_start + 9];
                        bms_info_data.ODO_AVG_Consumption = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    if (len > 11)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 10];
                        data_temp[1] = uart_temp.Data[data_start + 11];
                        bms_info_data.Gatheringline = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    if (len > 13)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 12];
                        data_temp[1] = uart_temp.Data[data_start + 13];
                        bms_info_data.Heating = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    if (len > 15)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 14];
                        data_temp[1] = uart_temp.Data[data_start + 15];
                        bms_info_data.PowerVoltage = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    break;
                }
                case GET_INFO_B:
                {
                    if (len > 1)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 0];
                        data_temp[1] = uart_temp.Data[data_start + 1];
                        bms_info_data.DeepCycleNum = data_temp[0] * 256 + data_temp[1] ;
                    }
                    if (len > 3)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 2];
                        data_temp[1] = uart_temp.Data[data_start + 3];
                        bms_info_data.TRIP = data_temp[0] * 256 + data_temp[1] ;
                    }
                    if (len > 5)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 4];
                        data_temp[1] = uart_temp.Data[data_start + 5];
                        bms_info_data.TRIP_Consumption = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    if (len > 7)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 6];
                        data_temp[1] = uart_temp.Data[data_start + 7];
                        bms_info_data.TRIP_AVG_Consumption = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    if (len > 9)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 8];
                        data_temp[1] = uart_temp.Data[data_start + 9];
                        bms_info_data.InSideCAN = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    if (len > 11)
                    {
                        data_temp[0] = uart_temp.Data[data_start + 10];
                        data_temp[1] = uart_temp.Data[data_start + 11];
                        bms_info_data.Cooling = (data_temp[0] * 256 + data_temp[1])  ;
                    }
                    break;
                }
                case GET_CELL_L1:
                {
                    for (i = 0; i < len; i++)
                    {
                        data_temp[0] = uart_temp.Data[data_start + i];
                        i++;
                        data_temp[1] = uart_temp.Data[data_start + i];
                        bes_cell_data.vol[0 + i / 2] = data_temp[0] * 256 + data_temp[1] ;
                    }
                    break;
                }
                case GET_CELL_L2:
                {
                    for (i = 0; i < len; i++)
                    {
                        data_temp[0] = uart_temp.Data[data_start + i];
                        i++;
                        data_temp[1] = uart_temp.Data[data_start + i];
                        bes_cell_data.vol[8 + i / 2] = data_temp[0] * 256 + data_temp[1] ;
                    }
                    break;
                }
                case GET_CELL_L3:
                {
                    for (i = 0; i < len; i++)
                    {
                        data_temp[0] = uart_temp.Data[data_start + i];
                        i++;
                        data_temp[1] = uart_temp.Data[data_start + i];
                        bes_cell_data.vol[16 + i / 2] = data_temp[0] * 256 + data_temp[1] ;
                    }
                    break;
                }
                case GET_CELL_L4:
                {
                    for (i = 0; i < len; i++)
                    {
                        data_temp[0] = uart_temp.Data[data_start + i];
                        i++;
                        data_temp[1] = uart_temp.Data[data_start + i];
                        bes_cell_data.vol[24 + i / 2] = data_temp[0] * 256 + data_temp[1] ;
                    }
                    break;
                }
                }
            }
        }
			}
    }
}
//***串口信息解析
void UART_Receive(void)
{
    if (s.uart0_receive_flag)
    {
        s.uart0_receive_flag = 0;
        s.ble_d = 0;
        Receive_process(uart_data0);
    }
    if (s.uart_blue_receive_flag)
    {
        s.uart_blue_receive_flag = 0;
        s.ble_d = 1;
        Receive_process(uart_data_blue);
    }
}

static void BLE_CMD_Process(uint8_t *p_data, uint8_t len)
{
    unsigned int ok, get_lenth = 0, i_num = 0, do_len, y_lop;
    int string_num, str2;
    char str[100];
    uint8_t speed_c, lop, i_lop;
    get_lenth = len;
    //**********************************************************
    if ((p_data[0] == 0x54) && (p_data[1] == 0x54) && (p_data[2] == 0x4D))
    {
        if (get_lenth > 10)
        {
           ;
        }
    }
    else
    {
        uint8_t  str_len = 0;
        string_num = Instr(p_data, get_lenth, "Scan End");
        if (string_num >= 0)
        {
            s.scaning_flag = 0;
        }
        else
        {
            while (str_len < get_lenth)
            {
                strncpy(str_temp, p_data + str_len, get_lenth - str_len);
                string_num = Instr(str_temp, get_lenth - str_len, "No.");
                if (string_num >= 0)
                {
                    str3 = Instr(str_temp, get_lenth - str_len, "\r\n");
                    str_len = str3 + 2;
                    if (str3 > 0)
                    {
                        if (str3 > 99)
                        {
                            str3 = 99;
                        }
                        Device_list[get_device_num].id = get_device_num;
                        strncpy(Device_list[get_device_num].list, str_temp, str3);
                        string_num = Instr(str_temp, get_lenth, "Name:");
                        if (string_num > 1)
                        {
                            str2 = Instr(str_temp + string_num, get_lenth - string_num, " ");
                            strncpy(Device_list[get_device_num].name, str_temp + string_num + 5, str2 - string_num);
                        }
                        string_num = Instr(str_temp, get_lenth, "Mac:");
                        if (string_num > 1)
                        {
                            strncpy(Device_list[get_device_num].mac, str_temp + string_num + 4, 12);
                            uint8_t i_num;
                            uint16_t data_h, data_l;
                            //**********************************************************************
                            for (i_num = 0; i_num < 12; i_num++)
                            {
                                if (Device_list[get_device_num].mac[i_num] > 0x60)
                                {
                                    data_h = Device_list[get_device_num].mac[i_num] - 0x60 + 9;
                                }
                                else if (Device_list[get_device_num].mac[i_num] > 0x40)
                                {
                                    data_h = Device_list[get_device_num].mac[i_num] - 0x40 + 9;
                                }
                                else
                                {
                                    data_h = Device_list[get_device_num].mac[i_num] - 0x30;
                                }
                                i_num = i_num + 1;
                                if (Device_list[get_device_num].mac[i_num] > 0x60)
                                {
                                    data_l = Device_list[get_device_num].mac[i_num] - 0x60 + 9;
                                }
                                else if (Device_list[get_device_num].mac[i_num] > 0x40)
                                {
                                    data_l = Device_list[get_device_num].mac[i_num] - 0x40 + 9;
                                }
                                else
                                {
                                    data_l = Device_list[get_device_num].mac[i_num] - 0x30;
                                }
                                Device_list[get_device_num].mac_hex[i_num / 2] = data_h * 16 + data_l;
                            }
                        }
                        get_device_num++;
                    }
                }
                else
                {
                    str_len = get_lenth;
                }
            }
        }
    }
}

void DMA_CH_Open2(uint32_t chn)
{
    DMA->CH[chn].CR |= (1 << DMA_CR_TXEN_Pos);
}

static void DMA_UART_TX_Config(uint8_t *send_buffer, uint16_t send_len)
{
    DMA_InitStructure DMA_initStruct;
    send_tx_len = send_len + 1 ;
    send_tx_time = 0;
    s.send_tx_dma = 1;
    DE_485_TX();//切换到发送状态
    systick_delay_ms(1);//延时1ms后再发送数据
    DMA_CH_Close(DMA_CH0);
    DMA_initStruct.Mode = DMA_MODE_SINGLE;
    DMA_initStruct.Unit = DMA_UNIT_BYTE;
    DMA_initStruct.Count = send_len;
    DMA_initStruct.SrcAddr = (uint32_t)send_buffer;
    DMA_initStruct.SrcAddrInc = 1;
    DMA_initStruct.DstAddr = (uint32_t)&UART0->DATA;
    DMA_initStruct.DstAddrInc = 0;
    DMA_initStruct.Handshake = DMA_CH0_UART0TX;
    DMA_initStruct.Priority = DMA_PRI_LOW;
    DMA_initStruct.INTEn = DMA_IT_DONE;
    DMA_CH_Init(DMA_CH0, &DMA_initStruct);
    DMA_CH_Open(DMA_CH0);
}
static void DMA_UART_BLUE_TX_Config(uint8_t *send_buffer, uint16_t send_len)
{
    DMA_InitStructure DMA_initStruct;

    DMA_initStruct.Mode = DMA_MODE_SINGLE;
    DMA_initStruct.Unit = DMA_UNIT_BYTE;
    DMA_initStruct.Count = send_len;
    DMA_initStruct.SrcAddr = (uint32_t)send_buffer;
    DMA_initStruct.SrcAddrInc = 1;
    DMA_initStruct.DstAddr = (uint32_t)&UART2->DATA;
    DMA_initStruct.DstAddrInc = 0;
    DMA_initStruct.Handshake = DMA_CH2_UART2TX;
    DMA_initStruct.Priority = DMA_PRI_LOW;
    DMA_initStruct.INTEn = DMA_IT_DONE;
    DMA_CH_Init(DMA_CH2, &DMA_initStruct);
    DMA_CH_INTClr(DMA_CH2, DMA_IT_DONE);
    DMA_CH_Open(DMA_CH2);
}

void isr_uart_dma_tx(void)
{
    if (DMA_CH_INTStat(DMA_CH0, DMA_IT_DONE))
    {
        DMA_CH_INTClr(DMA_CH0, DMA_IT_DONE);
        
    }
    if (DMA_CH_INTStat(DMA_CH2, DMA_IT_DONE))
    {
        DMA_CH_INTClr(DMA_CH2, DMA_IT_DONE);
        s.send_blue_tx_dma = 0;
    }
}
/*
*********************************************************************************************************
    函数原型：void UART_Send(void)
        描    述：UART数据发送
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void UART_Send(void)
{
    uint8_t get_send_index, i;
    if (s.send_run_flag) //300ms 触发一次
    {
        if (s.send_tx_dma == 0)
        {
            s.send_run_flag = 0;
            if (send_index < Send_order_data[current_page_index].Lenth)
            {
                send_index++;
            }
            else
            {
                send_index = 0;
            }
            if (send_index < Send_order_data[current_page_index].Lenth)
            {
                Get_send_order_data(current_page_index, send_index);
                Send_order_data[current_page_index].order_out_time[send_index]++;//发送次数 标记
                #if(BLUE_ENABLE)
                {
                    if (s.blue_connect)
                    {
                        DMA_UART_BLUE_TX_Config(Uart_send_data.Data, Uart_send_data.Lenth);
                    }
                    else
                    {
                        DMA_UART_TX_Config(Uart_send_data.Data, Uart_send_data.Lenth);
                    }
                }
                #else
                {
                    DMA_UART_TX_Config(Uart_send_data.Data, Uart_send_data.Lenth);
                }
                #endif
            }
        }
    }
}


static void Get_send_order_data(uint8_t page_index, uint8_t index)
{
    uint8_t send_data[20], order_len, i;
    uint16_t command, crc_cmd;
    command = Send_order_data[page_index].order[index];
    re_send_order = command;
    order_len = 8;
    send_data[0] = HEAD_ID;
    send_data[1] = 0x04;
    send_data[2] = (command >> 8);
    send_data[3] = command & 0xff;
    send_data[4] = 0;
    send_data[5] = Send_order_data[page_index].Get_data_len[index];//读取长度
    crc_cmd = CrcData(send_data, order_len - 2);
    send_data[6] =  crc_cmd & 0xff;
    send_data[7] = (crc_cmd >> 8);
    for (i = 0; i < order_len; i++)
    {
        Uart_send_data.Data[i] = send_data[i];
    }
//    #if(BLUE_ENABLE)
//    {
//        order_len = order_len + 2;
//        Uart_send_data.Data[i ] =  0x0D;
//        Uart_send_data.Data[i + 1] = 0X0A;
//    }
//    #endif
    Uart_send_data.Lenth = order_len;
}


/*
*********************************************************************************************************
    函数原型：void Data_get_from_uart_process(void)
        描    述：定时获取当前页面的数据信息，通过定时发送当前页面的命令，来获取当前页面的信息
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void Data_get_from_uart_process(void)
{
}
void Ble_get_process(void)
{
    #if(BLUE_ENABLE)
    
    //    //******启动扫描*******************
    if (s.scan_start_flag)
    {
        if (s.blue_connect)
        {
            send_BLE_DISCON_order();
        }
        else
        {
            s.scan_start_flag = 0;
            s.scaning_flag = 1;
            send_BLE_Scan_order();
        }
    }
    //******启动连接*******************
    if (s.connect_start)
    {
        if (s.scaning_flag)
        {
            send_BLE_Stop_Scan_order();
        }
        else
        {
            s.connect_start = 0;
            send_BLE_Connect_order(def_device);
        }
    }
    //******自动连接*******************
    if (s.def_mac_flag)
    {
        if ((s.blue_connect == 0) && (s.scan_start_flag == 0))
        {
            if (s.scaning_flag == 0)
            {
                auto_connect_time++;
                if (auto_connect_time > 3) //每2秒尝试连接一次
                {
                    auto_connect_time = 0;
                    wait_out_time++;
                    s.connect_start = 1;
                    if (wait_out_time > 5)
                    {
                        s.ble_off_line = 1;
                    }
                }
            }
        }
    }
    #endif
}
static void Get_bms_info(uint16_t cmd)
{
}

/*
*********************************************************************************************************
    函数原型：void led_debug_init(void)
        描    述：指示灯端口初始化
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void led_debug_init(void)
{
    GPIO_Init(GPIOM, PIN2, 1, 1, 0, 0);
}

/*
*********************************************************************************************************
    函数原型：void LED_ON(void)
        描    述：指示灯控制开
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void LED_ON(void)
{
    GPIO_AtomicSetBit(GPIOM, PIN2); /* 点亮LED光 */
}
/*
*********************************************************************************************************
    函数原型：void LED_OFF(void)
        描    述：指示灯控制关
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void LED_OFF(void)
{
    GPIO_AtomicClrBit(GPIOM, PIN2); /* 熄灭LED光 */
}
/*
*********************************************************************************************************
    函数原型：void buzzer_init(void)
        描    述：蜂鸣器端口初始化
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void buzzer_init(void)
{
    PORT_Init(PORTD, PIN2, PORTD_PIN2_GPIO, 0);
    GPIO_Init(GPIOD, PIN2, 1, 1, 0, 0);
}

/*
*********************************************************************************************************
    函数原型：void LED_ON(void)
        描    述：指示灯控制开
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void BUZZER_ON(void)
{
    GPIO_AtomicSetBit(GPIOD, PIN2); /* 开启蜂鸣器 */
}
/*
*********************************************************************************************************
    函数原型：void LED_OFF(void)
        描    述：指示灯控制关
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void BUZZER_OFF(void)
{
    GPIO_AtomicClrBit(GPIOD, PIN2); /* 关闭蜂鸣器 */
}
/*
*********************************************************************************************************
    函数原型：void buzzer_init(void)
        描    述：蜂鸣器端口初始化
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void BLUE_uart_init(void)
{
    PORT_Init(PORTA, PIN8, PORTA_PIN8_GPIO, 0);
    GPIO_Init(GPIOA, PIN8, 1, 1, 0, 0);
    PORT_Init(PORTD, PIN1, PORTD_PIN1_GPIO, 0);
    GPIO_Init(GPIOD, PIN1, 0, 1, 0, 0);//BLZ 信号，低电平有效
    #if BLUE_ENABLE
    uart_blue_init();
    RF_OFF();
    #endif
}

/*
*********************************************************************************************************
    函数原型：void LED_ON(void)
        描    述：指示灯控制开
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void RF_ON(void)
{
    GPIO_AtomicSetBit(GPIOA, PIN8); /* 开启蜂鸣器 */
}
/*
*********************************************************************************************************
    函数原型：void LED_OFF(void)
        描    述：指示灯控制关
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void RF_OFF(void)
{
    GPIO_AtomicClrBit(GPIOA, PIN8); /* 关闭蜂鸣器 */
}

/*
*********************************************************************************************************
    函数原型：void LED_ON(void)
        描    述：指示灯控制开
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void DE_485_TX(void)
{
    GPIO_AtomicSetBit(GPIOM, PIN3); /* 发送状态 */
}
/*
*********************************************************************************************************
    函数原型：void LED_OFF(void)
        描    述：指示灯控制关
    输入参数：-
    输出参数：-
*********************************************************************************************************
*/
void DE_485_RX(void)
{
    GPIO_AtomicClrBit(GPIOM, PIN3); /* 接收状态 */
}

void Buzzer_cotrl(unsigned int per_time)
{
    if (per_time)
    {
        BUZZER_ON();
        s.buzzer = 1;
        buzzer_time = 0;
        buzzer_set_time = per_time;
    }
}



unsigned int Getlen(const char *p1)
{
    unsigned int  lop;
    for (lop = 0; (* (p1 + lop) != 0) && (lop < 255); lop++);
    return lop;
}
int Instr(uint8_t *p1, unsigned int  len,  char *p2)
{
    unsigned int  lop = 0, y;
    unsigned int  str1_lenth, str2_lenth;
    str1_lenth = Getlen(p1);
    str2_lenth = Getlen(p2);
    if (len > str2_lenth)
    {
        len = str2_lenth;
    }
    if (str1_lenth < len)
    {
        return -1;
    }
    for (lop = 0; lop < str1_lenth; lop++)
    {
        if (*(p1 + lop) == *p2)
        {
            for (y = 0; y < len; y++)
            {
                if (*(p1 + lop + y) != *(p2 + y))
                {
                    break;
                }
            }
            if (y == len)
            {
                return lop;
            }
        }
    }
    return -1;
}
//***********************************************************
void send_BLE_Reset_order(void)
{
    #if(BLUE_ENABLE)
    {
        uint8_t i, len;
        uint8_t ble_send_data[30];
        len = Getlen(BLE_RESET);
        // len = 19;
        for (i = 0; i < len; i++)
        {
            ble_send_data[i] = BLE_RESET[i];
        }
        DMA_UART_BLUE_TX_Config(ble_send_data, len);
        //uart_send_msg(UART2,ble_send_data,len);
    }
    #endif
}
void send_BLE_Scan_order(void)
{
    #if(BLUE_ENABLE)
    {
        uint8_t i, len;
        uint8_t ble_send_data[30];
        char *order = "TTM:SCA-?";
        len = Getlen(order);
        // len = 19;
        for (i = 0; i < len; i++)
        {
            ble_send_data[i] = order[i];
        }
        DMA_UART_BLUE_TX_Config(ble_send_data, len);
        //uart_send_msg(UART2,ble_send_data,len);
    }
    #endif
}
void send_BLE_Stop_Scan_order(void)
{
    #if(BLUE_ENABLE)
    {
        uint8_t i, len;
        uint8_t ble_send_data[30];
        char *order = "TTM:DISCA-";
        len = Getlen(order);
        // len = 19;
        for (i = 0; i < len; i++)
        {
            ble_send_data[i] = order[i];
        }
        DMA_UART_BLUE_TX_Config(ble_send_data, len);
        //uart_send_msg(UART2,ble_send_data,len);
    }
    #endif
}
void send_BLE_Connect_order(def_ble_device_type ble_device)
{
    #if(BLUE_ENABLE)
    {
        uint8_t i, len;
        uint8_t ble_send_data[30];
        char str[100];
        //uart_send_msg(UART2,ble_send_data,len);
        if (s.def_mac_flag == 0)
        {
            eeprom_write(0, 0xaa);
        }

        for (i = 0; i < 6; i++)
        {
            if (eeprom_def_device.mac_hex[i] != ble_device.mac_hex[i])
            {
                break;
            }
        }
        if (i < 6)
        {
            for (i = 0; i < 6; i++)
            {
                eeprom_write(1 + i, ble_device.mac_hex[i]);
                def_device.mac_hex[i] = ble_device.mac_hex[i];
            }
            len = Getlen(ble_device.name);
            eeprom_write(10, len);
            for (i = 0; i < len; i++)
            {
                eeprom_write(11 + i, ble_device.name[i]);
                def_device.name[i] = ble_device.name[i];
            }
        }
        sprintf(str, "_%02X%02X%02X%02X%02X%02X",
                ble_device.mac_hex[0],
                ble_device.mac_hex[1],
                ble_device.mac_hex[2],
                ble_device.mac_hex[3],
                ble_device.mac_hex[4],
                ble_device.mac_hex[5]
               );
        strcpy(dsp_ver_info[1], ble_device.name);

        strcat(dsp_ver_info[1], str);
        strcat(dsp_ver_info[1], "\0");
        //********************************************************
        char *order = "TTM:COND-";
        len = Getlen(order);
        // len = 19;
        for (i = 0; i < len; i++)
        {
            ble_send_data[i] = order[i];
        }
        for (; i < len + 6; i++)
        {
            ble_send_data[i] = ble_device.mac_hex[i - len];
        }
        DMA_UART_BLUE_TX_Config(ble_send_data, i);
    }
    #endif
}
static void send_BLE_GET_NAME_order(void)
{
    #if(BLUE_ENABLE)
    uint8_t i, len;
    uint8_t ble_send_data[30];
    len = Getlen(BLE_GET_NAME);
    //len = 9;
    for (i = 0; i < len; i++)
    {
        ble_send_data[i] = BLE_GET_NAME[i];
    }
    DMA_UART_BLUE_TX_Config(ble_send_data, len);
    //uart_send_msg(UART2,ble_send_data,len);
    #endif
}
static void send_BLE_GET_MAC_order(void)
{
    #if(BLUE_ENABLE)
    uint8_t i, len;
    uint8_t ble_send_data[30];
    len = Getlen(GET_MAC);
    //len = 9;
    for (i = 0; i < len; i++)
    {
        ble_send_data[i] = GET_MAC[i];
    }
    DMA_UART_BLUE_TX_Config(ble_send_data, len);
    //uart_send_msg(UART2,ble_send_data,len);
    #endif
}
//*******蓝牙广播头信息更改*************************************************
void send_BLE_ADD_order(unsigned char *MAC_DATA)
{
    #if(BLUE_ENABLE)
    #endif
}
//*******蓝牙名称更改*************************************************
void send_BLE_NAME_order( char *DEF_NAME)
{
    #if(BLUE_ENABLE)
    uint8_t i, j, len, len2;
    uint8_t ble_send_data[40];
    len = Getlen(BLE_SET_NAME);
    for (i = 0; i < len; i++)
    {
        ble_send_data[i] = BLE_SET_NAME[i];
    }
    len2 = Getlen(DEF_NAME);
    for (j = 0; j < len2; j++)
    {
        ble_send_data[i + j] = DEF_NAME[j];
    }
    DMA_UART_BLUE_TX_Config(ble_send_data, len + len2);
    #endif
}
//*******蓝牙断开*************************************************
void send_BLE_DISCON_order(void)
{
    #if(BLUE_ENABLE)
    uint8_t i, j, len, len2;
    uint8_t ble_send_data[40];
    char *order = "TTM:DISCON-";
    len = Getlen(order);
    for (j = 0; j < len; j++)
    {
        ble_send_data[i + j] = order[j];
    }
    DMA_UART_BLUE_TX_Config(ble_send_data, len);
    #endif
}


#define EEPROM_ADDR   0x7D000

uint32_t WrBuff[32] = {0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
                       0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
                       0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
                       0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
                      };

uint32_t eeprom_read_byte(uint32_t addr)
{
    uint32_t p = *((volatile uint32_t *)(EEPROM_ADDR + addr * 4));
    return p;
}
void eeprom_write(uint32_t addr, uint8_t data)
{
    uint16_t lop;
    for (lop = 0; lop < 32; lop++)
    {
        WrBuff[lop] = *((volatile uint32_t *)(EEPROM_ADDR + lop * 4));
    }
    FLASH_Erase(EEPROM_ADDR);
    WrBuff[addr] = data;
    FLASH_Write(EEPROM_ADDR, WrBuff, 32);
}

void config_read(void)
{
    uint8_t name_lenth, lop;
    s.blue_connect = 0;
    s.scan_start_flag = 0;
    s.scaning_flag = 0;
    s.connect_start = 0;
    wait_out_time = 0;
    s.ble_off_line = 0;
    //************************************************
    if (eeprom_read_byte(0) == 0xaa)
    {
        s.def_mac_flag = 1;
        for (lop = 0; lop < 6; lop++)
        {
            def_device.mac_hex[lop] = eeprom_read_byte(1 + lop);
            eeprom_def_device.mac_hex[lop] = def_device.mac_hex[lop] ;
        }
        name_lenth = eeprom_read_byte(10);
        if (name_lenth < 20)
        {
            for (lop = 0; lop < name_lenth; lop++)
            {
                def_device.name[lop] = eeprom_read_byte(11 + lop);
                eeprom_def_device.name[lop] =  def_device.name[lop];
            }
        }
        auto_connect_time = 5;
    }
    else
    {
        s.def_mac_flag = 0;
    }
    s.page_index_change = 0;
}

void    Clear_page_data(void)
{
    //页面原始数据清零
    if (s.clear_page_data)
    {
        s.clear_page_data = 0;
        switch (current_page_index)
        {
        case 0:
        {
            memset(&bms_main_data, 0, sizeof(bms_main_data));
            break;
        }
        case 1:
        {
            memset(&bms_info_data, 0, sizeof(bms_info_data));
            break;
        }
        case 2:
        {
            memset(&bes_cell_data, 0, sizeof(bes_cell_data));
            break;
        }
        case 3:
        {
            memset(&bms_version_data, 0, sizeof(bms_version_data));
            break;
        }
        }
    }
    //*********页面切换触发*******
    if (s.get_data_flag)
    {
        s.get_data_flag = 0;
        s.off_line = 0;
    }
}
